module.exports = function ($scope,$stateParams,RequestService,$localStorage,modelService,commonConfig,$modal,$modalInstance,data) {
    $scope.User = $localStorage.get('cloud');
    //获取应用类型
    RequestService.Request(commonConfig.cloudApi+'cloud/apptype','GET').then(function(res){
        // console.log(res);
        $scope.AppTypeList = res;
    },function(error){
        console.log(error);
    });
    //获取用户对象类型
    RequestService.Request(commonConfig.cloudApi+'sys/usertype?tenantId='+$scope.User.detail.tenantId,'GET').then(function(res){
        // console.log(res);
        $scope.UserType = res;
    },function(error){
        console.log(error);
    });
    //获取厂商类型
    RequestService.Request(commonConfig.cloudApi+'cloud/company/getCompanyByUserId?userId='+$scope.User.detail.id,'GET').then(function(res){
        // console.log(res);
        $scope.Company = res;
        //数据初始化
        if(data){
            console.log(data);
            $scope.form = {
                "id": data.id,
                "appName": data.appName,
                "appNo": data.appNo,
                "appTypeId": data.appTypeId,
                "companyId": data.companyId,
                "icon": data.icon,
                "isOpen": data.isOpen,
                "price": data.price,
                "remark": data.remark,
                "status": data.status,
                "url": data.url,
                "mobileUrl":data.mobileUrl,
                "useObject": null,
                "versionNo": data.versionNo,
                "target": data.target
            };
            $scope.userObjectArr = data.useObject.split(',');
            // checkBox默认选中
            //被选中条件：ng-checked的结果为true
            $scope.isSelected = function(name){
                for(var i=0; i< $scope.userObjectArr.length;i++){
                    if($scope.userObjectArr[i] == name){
                        return true
                    }
                }
                return false;
                //将result与checkbox的当前id即value值匹配
                //有就返回的true，没有就返回false
            };
            //对空数组进行初始化绑定，可以加入已选的用户
            angular.forEach($scope.userObjectArr,function(a,i){
                angular.forEach($scope.UserType,function(b,j){
                    if(a == b.userTypeName){
                        checkArr.push(b.id)
                    }
                })
            });
        }else {
            $scope.form = {
                "id": 0,
                "appName": "",
                "appNo": null,
                "appTypeId": null,
                "companyId": $scope.Company.id,
                "icon": "",
                "isOpen": null,
                "price": null,
                "remark": "",
                "status": 0,
                "url": "",
                "mobileUrl":"",
                "useObject": "",
                "versionNo": "",
                "target": false
            };
        }
    },function(error){
        console.log(error);
    });

    var checkArr = [];
    $scope.setCheck = function(checkId){
        if(checkArr.length == 0){
            checkArr.push(checkId)
        }else{
            var flag = false;
            for(var i = 0;i< checkArr.length;i++){
                if( checkArr[i] == checkId){
                    checkArr.splice(i,1);
                    flag = true;
                    break;
                }
            }
            if(!flag){
                checkArr.push(checkId);
            }
        }
        console.log(checkArr);
    };

    // if (data) { //当对已有APP进行修改
    //     RequestService.Request(commonConfig.cloudApi+'cloud/app/' + data.id, 'GET').then(function (res) {
    //         console.log(res);
    //         $scope.form = res;
    //         $scope.userObjectArr = res.useObject.split(',');
    //         //checkBox默认选中
    //         //被选中条件：ng-checked的结果为true
    //         $scope.isSelected = function(name){
    //             for(var i=0; i< $scope.userObjectArr.length;i++){
    //                 if($scope.userObjectArr[i] == name){
    //                     return true
    //                 }
    //             }
    //             return false;
    //             //将result与checkbox的当前id即value值匹配
    //             //有就返回的true，没有就返回false
    //         };
    //
    //         //对空数组进行初始化绑定，可以加入已选的用户
    //         angular.forEach($scope.userObjectArr,function(a,i){
    //             angular.forEach($scope.UserType,function(b,j){
    //                 if(a == b.userTypeName){
    //                     checkArr.push(b.id)
    //                 }
    //             })
    //         });
    //         //获取常用应用列表
    //         getCommonAppFunctions();
    //         // console.log(checkArr);
    //     }, function (error) {
    //         console.log(error);
    //     });
    // }




    // //图片上传设置
    // var uploader = $scope.uploader = new FileUploader({
    //     url: commonConfig.$webroot+commonConfig.dfsApi+'file?access_token='+$scope.User.token,  //固定的地址
    //     queueLimit: 1 //文件个数限制
    //     //removeAfterUpload: true //上传后删除文件
    // });
    // // FILTERS
    // uploader.filters.push({
    //     name: 'imageFilter',
    //     fn: function (item /*{File|FileLikeObject}*/, options) {
    //         var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
    //         return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
    //     }
    // });
    // uploader.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/, filter, options) {
    //     console.info('添加文件到上传队列失败后', item, filter, options);
    // };
    // uploader.onAfterAddingAll = function (addedFileItems) {
    //     $scope.uploader.uploadAll();
    //     console.info('添加所选的所有文件到上传队列后', addedFileItems);
    // };
    // uploader.onCompleteItem = function (fileItem, response, status, headers) {
    //     console.info('文件上传完成后', fileItem, response, status, headers);
    //     $scope.imgUrl = response.uuid;
    //     console.info("$scope.imgUrl:", $scope.imgUrl);
    // };
    //
    //
    // //应用详情图
    // $scope.BannerUrl = [];
    // var uploader2 = $scope.uploader2 = new FileUploader({
    //     url: commonConfig.$webroot+commonConfig.dfsApi+'file?access_token='+$scope.User.token  //固定的地址
    //     //queueLimit: 1 //文件个数限制
    //     //removeAfterUpload: true //上传后删除文件
    // });
    // // FILTERS
    // uploader2.filters.push({
    //     name: 'imageFilter',
    //     fn: function (item /*{File|FileLikeObject}*/, options) {
    //         var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
    //         return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
    //     }
    // });
    // uploader2.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/, filter, options) {
    //     console.info('添加文件到上传队列失败后', item, filter, options);
    // };
    // uploader2.onAfterAddingAll = function (addedFileItems) {
    //     $scope.uploader2.uploadAll();
    //     console.info('添加所选的所有文件到上传队列后', addedFileItems);
    // };
    // uploader2.onCompleteItem = function (fileItem, response, status, headers) {
    //     console.info('文件上传完成后', fileItem, response, status, headers);
    //     $scope.BannerUrl = $scope.BannerUrl.concat(response.uuid);
    //     console.info("$scope.BannerUrl:", $scope.BannerUrl);
    // };
    //
    // function resetApp(){
    //     //修改App
    //     RequestService.Request(commonConfig.cloudApi+'cloud/app/' + $stateParams.id, 'PUT', $scope.form).then(function (res) {
    //         console.log(res);
    //         if(res.code == 0){
    //             if($scope.BannerUrl.length == 0){ //判断有无上传轮播图
    //                 modelService.openTipsModel('提交成功！','logo.DeveloperApps'); //成功并跳转
    //             }else {
    //                 setAppBanners(); //上传轮播图
    //             }
    //         }else {
    //             modelService.openTipsModel(res.data.message);
    //         }
    //     }, function (error) {
    //         console.log(error);
    //     });
    // }
    // //设置图片上传路径
    // function setAppBanners(){
    //     $scope.form.appPhotoList = [];
    //     angular.forEach($scope.BannerUrl,function(val,i){
    //         if ($stateParams.id) {
    //             var Banner = {
    //                 "appId": $stateParams.id,
    //                 "appPhotoFile": val,
    //                 "appPhotoNo": i+1,
    //                 "id": 0
    //             };
    //         }else {
    //             var Banner = {
    //                 "appId": 0,
    //                 "appPhotoFile": val,
    //                 "appPhotoNo": i+1,
    //                 "id": 0
    //             };
    //         }
    //
    //         $scope.form.appPhotoList.push(Banner);
    //     });
    //     console.log($scope.form);
    //     if ($stateParams.id) {  //修改信息（只能实现新增图片，不能删除原来的图片） TODO:预留轮播图模块（选择覆盖还是新增还是删除？）
    //         RequestService.Request(commonConfig.cloudApi+'cloud/appphoto/insertAppPhotoList','POST', $scope.form.appPhotoList).then(function (res) {
    //             console.log(res);
    //             if(res.code == 0 ){
    //                 modelService.openTipsModel('提交成功！','logo.DeveloperApps'); //成功并跳转
    //             }else {
    //                 modelService.openTipsModel(res.message);
    //             };
    //         }, function (error) {
    //             console.log(error);
    //         });
    //     }else {     //新增信息（包括新增图片）
    //         if($scope.form.appCommonlyFunctionList.length == 0){
    //             delete $scope.form.appCommonlyFunctionList;
    //             RequestService.Request(commonConfig.cloudApi+'cloud/app/insertAppWithPhotos','POST', $scope.form).then(function (res) {
    //                 console.log(res);
    //                 if(res.code == 0 ){
    //                     modelService.openTipsModel('提交成功！','logo.DeveloperApps'); //成功并跳转
    //                 }else {
    //                     modelService.openTipsModel(res.message);
    //                 };
    //             }, function (error) {
    //                 console.log(error);
    //             });
    //         }else {
    //             //若有存在常用应用模块
    //             setCommonAppFunctions();
    //         }
    //
    //     }
    // }
    //
    // /******************************************************常用功能******************************************************/
    // $scope.AddCommonFun = function(){
    //      $modal.open({
    //         animation: true,
    //         templateUrl: './views/model/CommonFunctionsModel.html',
    //         size: 'md',
    //         scope:$scope,
    //         controller: function($scope,$modalInstance,FileUploader) {
    //             //初始化数据
    //             $scope.commonApp = {
    //                 "appCommonlyFunctionIcon":"",
    //                 "appCommonlyFunctionName":"",
    //                 "appCommonlyFunctionNo": null,
    //                 "appCommonlyFunctionUrl": "",
    //                 "appId": $stateParams.id? parseInt($stateParams.id):0,
    //                 "id": 0
    //             };
    //             //图片上传设置
    //             var uploader3 = $scope.uploader3 = new FileUploader({
    //                 url: commonConfig.$webroot+commonConfig.dfsApi+'file?access_token='+$scope.User.token ,  //固定的地址
    //                 queueLimit: 1 //文件个数限制
    //                 //removeAfterUpload: true //上传后删除文件
    //             });
    //             // FILTERS
    //             uploader3.filters.push({
    //                 name: 'imageFilter',
    //                 fn: function (item /*{File|FileLikeObject}*/, options) {
    //                     var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
    //                     return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
    //                 }
    //             });
    //             uploader3.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/, filter, options) {
    //                 console.info('添加文件到上传队列失败后', item, filter, options);
    //             };
    //             uploader3.onAfterAddingAll = function (addedFileItems) {
    //                 $scope.uploader3.uploadAll();
    //                 console.info('添加所选的所有文件到上传队列后', addedFileItems);
    //             };
    //             uploader3.onCompleteItem = function (fileItem, response, status, headers) {
    //                 console.info('文件上传完成后', fileItem, response, status, headers);
    //                 $scope.commonApp.appCommonlyFunctionIcon = response.uuid;
    //                 console.info("$scope.CommonFunImgUrl:", $scope.commonApp.appCommonlyFunctionIcon);
    //             };
    //             $scope.ok = function(){
    //                 $scope.form.appCommonlyFunctionList.push($scope.commonApp);
    //                 console.log($scope.form);
    //                 $modalInstance.close();
    //             };
    //             $scope.cancel = function(){
    //                 $modalInstance.close();
    //             }
    //         }
    //     });
    // };
    //
    //
    // //去除选择
    // $scope.unSelectCommonFun = function(appCommonlyFunctionName){
    //     angular.forEach($scope.form.appCommonlyFunctionList,function(val,i){
    //         if(appCommonlyFunctionName == val.appCommonlyFunctionName){
    //             $scope.form.appCommonlyFunctionList.splice(i,1);
    //         }
    //     });
    //     console.log($scope.form.appCommonlyFunctionList);
    // };
    // function setCommonAppFunctions(){
    //     console.log('添加常用应用');
    //     RequestService.Request(commonConfig.cloudApi+'cloud/app/insertAppWithPhotosAndCommonlyFunction','POST', $scope.form).then(function (res) {
    //         console.log(res);
    //         if(res.code == 0 ){
    //             modelService.openTipsModel('提交成功！','logo.DeveloperApps'); //成功并跳转
    //         }else {
    //             modelService.openTipsModel(res.message);
    //         };
    //     }, function (error) {
    //         console.log(error);
    //     });
    // }
    // function getCommonAppFunctions(){
    //     RequestService.Request(commonConfig.cloudApi+'cloud/appcommonlyfunction?appId=' + $stateParams.id, 'GET').then(function (res) {
    //         console.log(res);
    //         $scope.form.appCommonlyFunctionList = res;
    //     }, function (error) {
    //         console.log(error);
    //     });
    // }
    // function resetCommonAppFunctions(){
    //     RequestService.Request(commonConfig.cloudApi+'cloud/appcommonlyfunction/insertAppCommonlyFunctionList?appId='+$stateParams.id,'POST', $scope.form.appCommonlyFunctionList).then(function (res) {
    //         console.log(res);
    //         if(res.code == 0){
    //             delete $scope.form.appCommonlyFunctionList;
    //             resetApp();
    //         }else {
    //             modelService.openTipsModel(res.message);
    //         };
    //     }, function (error) {
    //         console.log(error);
    //     });
    // }



    $scope.ok = function () {  //提交应用
        $scope.form.useObject = checkArr.join(',');   //应用对象数据处理
        RequestService.Request(commonConfig.cloudApi+'cloud/app/' + (data ? data.id: ''), data ? 'PUT' : 'POST', $scope.form).then(function (res) {
            console.log(res);
            if(res.code == 0){
                $modalInstance.close();
            }else {
                modelService.openTipsModel('提交失败！');
            }
        }, function (error) {
            console.log(error);
        });
    };

    //关闭模态框
    $scope.cancel = function () {
        $modalInstance.dismiss();
    }
};